package com.fhz.mvvm.ui.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;

import com.fhz.mvvm.R;
import com.fhz.mvvm.callback.ApiResponse;
import com.fhz.mvvm.databinding.ActivityLoginBinding;
import com.fhz.mvvm.entity.HUser;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class LoginActivity extends AppCompatActivity {

    LoginViewModel mLoginViewModel;

    ActivityLoginBinding mBinding;
    private static final String TAG = "LoginActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        String username = getIntent().getStringExtra("username");
        System.out.println(username);

        mLoginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);


        //输入框监听器
        TextWatcher textWatcher = new TextWatcher(){
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d(TAG, "beforeTextChanged: ");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d(TAG, "onTextChanged: ");
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!mBinding.loginPassword.getText().toString().equals("") && !mBinding.loginUsername.getText().toString().equals("") ){
                    mBinding.loginBtn.setEnabled(true);
                    mBinding.registerBtn.setEnabled(true);
                }else{
                    mBinding.registerBtn.setEnabled(false);
                }
            }
        };
        mBinding.loginUsername.addTextChangedListener(textWatcher);
        mBinding.loginPassword.addTextChangedListener(textWatcher);


        mBinding.loginBtn.setOnClickListener(v ->{

        });
        mBinding.registerBtn.setOnClickListener(v ->{
            HUser hUser = new HUser();
            hUser.setNickname( mBinding.loginUsername.getText().toString());
            hUser.setPassword(mBinding.loginPassword.getText().toString());

            mLoginViewModel.register(hUser).observe(this, new Observer<ApiResponse<HUser>>() {
                @Override
                public void onChanged(ApiResponse<HUser> hUserApiResponse) {
                    System.out.println(hUserApiResponse.getData().getNickname());
                }
            });
        });

    }

    public static void launch(Context context,String username){
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra("username","");
        context.startActivity(intent);
    }

}